//
// Created by DELL on 2021/3/18.
//

#ifndef SYLAR_TCP_SERVER_H
#define SYLAR_TCP_SERVER_H

#include <memory>
#include <functional>
#include "iomanager.h"
#include "socket.h"
#include "noncopyable.h"

namespace sylar {
    class TcpServer : public std::enable_shared_from_this<TcpServer>, Noncopyable {
    public:
        using ptr = std::shared_ptr<TcpServer>;

        explicit TcpServer(sylar::IOManager* worker = sylar::IOManager::GetThis()
                ,sylar::IOManager* io_worker = sylar::IOManager::GetThis()
                ,sylar::IOManager* accept_worker = sylar::IOManager::GetThis());

        virtual ~TcpServer();

        virtual bool bind(sylar::Address::ptr addr);

        virtual bool bind(const std::vector<Address::ptr> &addrs, std::vector<Address::ptr> &fails);

        virtual bool start();

        virtual void stop();

        uint64_t getReadTimeout() const { return m_recvTimeout; }

        std::string getName() const { return m_name; }

        void setReadTimeout(uint64_t v) { m_recvTimeout = v; }

        virtual void setName(const std::string &v) { m_name = v; }

        bool isStop() const { return m_isStop; }

    protected:
        virtual void handleClient(Socket::ptr client);

        virtual void startAccept(Socket::ptr sock);

    protected:
        /// 监听Socket数组
        std::vector<Socket::ptr> m_socks;
        /// 新连接的Socket工作的调度器
        IOManager* m_worker;
        IOManager* m_ioWorker;
        /// 服务器Socket接收连接的调度器
        IOManager* m_acceptWorker;
        /// 接收超时时间(毫秒)
        uint64_t m_recvTimeout;
        /// 服务器名称
        std::string m_name;
        /// 服务器类型
        std::string m_type = "tcp";
        /// 服务是否停止
        bool m_isStop;

        bool m_ssl = false;

//        TcpServerConf::ptr m_conf;
    };
}
#endif //SYLAR_TCP_SERVER_H
